//
//  ShufflingView.m
//  ToolFramework
//
//  Created by apple on 2018/2/10.
//  Copyright © 2018年 apple. All rights reserved.
//
#define MAS_SHORTHAND
#define MAS_SHORTHAND_GLOBALS
#define WBScreenWidth [UIScreen mainScreen].bounds.size.width
#import "ShufflingView.h"
#import "AnnouncementDetailController.h"

@implementation ShufflingView
+(void)addShufflingViewToView:(UIView *)view viewHeight:(float)height data:(NSArray *)data controller:(UIViewController *)vc{
   
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    [view addSubview:scrollView];
    [scrollView makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(view);
    }];
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.contentSize = CGSizeMake(WBScreenWidth, height*data.count);
    scrollView.pagingEnabled = YES;
    
    
    ShufflingView *shuf = [ShufflingView new];
    shuf.scrollView = scrollView;
    shuf.scHeight = height;
    shuf.dataArr = data;
    shuf.otherVC = vc;
    for (int i=0; i<data.count; i++) {
        UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, height*i, WBScreenWidth, height)];
        [scrollView addSubview:bgView];
        
        UIImageView *imgView = [[UIImageView alloc] init];
        [imgView setImage:[UIImage imageNamed:@"speaker"]];
        [bgView addSubview:imgView];
        [imgView makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(bgView);
            make.left.equalTo(bgView).offset(16);
            make.height.width.equalTo(13);
        }];
        
        
        UILabel *titleLab = [[UILabel alloc] init];
        [bgView addSubview:titleLab];
        titleLab.text = @"【公告】";
        [titleLab setFont:[UIFont systemFontOfSize:12]];
        [titleLab setTextColor:[UIColor colorWithRed:220/255.0 green:60/255.0 blue:65/255.0 alpha:1]];
        [titleLab makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(bgView);
            make.left.equalTo(imgView.mas_right).offset(7);
            make.top.bottom.equalTo(bgView);
            make.width.equalTo(50);
        }];
        
        
        UILabel *contentLab = [[UILabel alloc] init];
        [bgView addSubview:contentLab];
        [contentLab makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(bgView);
            make.left.equalTo(titleLab.mas_right).offset(7);
            make.top.bottom.equalTo(bgView);
            make.right.equalTo(13);
        }];

        NSString *title = data[i];

        contentLab.text = title;
        [contentLab setFont:[UIFont systemFontOfSize:11]];
        [contentLab setTextColor:[UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1]];
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.tag = i;
        btn.titleLabel.text = @" ";
        btn.backgroundColor = [UIColor clearColor];
        [btn addTarget:shuf action:@selector(pushToNextPage:) forControlEvents:UIControlEventTouchUpInside];
        [bgView addSubview:btn];
        
        [btn makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.bottom.equalTo(bgView);
        }];
                         
    }
    [NSTimer scheduledTimerWithTimeInterval:2 target:shuf selector:@selector(moveScrollView) userInfo:nil repeats:YES];
    
}
-(void)pushToNextPage:(UIButton *)btn{
    int index = (int)btn.tag;
    AnnouncementDetailController *detailVC = [AnnouncementDetailController new];
    detailVC.hidesBottomBarWhenPushed = YES;
    NSString *loadurl = [NSString stringWithFormat:@"news%d",index+1];
    detailVC.url = loadurl;
    [self.otherVC.navigationController pushViewController:detailVC animated:NO];
}

-(void)moveScrollView{
    
    _count ++;
    _count= _count == self.dataArr.count ? 0:_count;
    self.scrollView.contentOffset = CGPointMake(0, self.scHeight*_count);
  
}
@end
